package com.huanxing.cloud.mall.api.handler.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.huanxing.cloud.common.core.constant.CacheConstants;
import com.huanxing.cloud.common.core.constant.SecurityConstants;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.common.myabtis.tenant.HxTenantContextHolder;
import com.huanxing.cloud.mall.api.service.IUserInfoService;
import com.huanxing.cloud.mall.common.dto.HxTokenInfo;
import com.huanxing.cloud.mall.common.dto.UserLoginDTO;
import com.huanxing.cloud.mall.common.entity.UserInfo;
import com.huanxing.cloud.mall.common.feign.FeignWxAppService;
import com.huanxing.cloud.mall.common.feign.FeignWxUserService;
import com.huanxing.cloud.miniapp.common.dto.WxUserDTO;
import com.huanxing.cloud.miniapp.common.entity.WxApp;
import com.huanxing.cloud.miniapp.common.entity.WxUser;
import com.huanxing.cloud.security.handler.HxBusinessException;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component("WX_MA_LOGIN")
@AllArgsConstructor
public class WechatLoginHandler extends AbstractLoginHandler {

	private final IUserInfoService userInfoService;

	private final FeignWxAppService feignWxAppService;

	private final FeignWxUserService feignWxUserService;

	private final RedisTemplate<String, String> redisTemplate;

	@Override
	public void before(UserLoginDTO userLoginDTO) {
		Result<WxApp> wxAppResult = feignWxAppService.getById(userLoginDTO.getAppId(), SecurityConstants.SOURCE_IN);
		if (Boolean.FALSE.equals(wxAppResult.isOk())) {
			throw new HxBusinessException(wxAppResult.getMsg());
		}
		WxApp WxApp = wxAppResult.getData();
		if (ObjectUtil.isNull(WxApp)) {
			throw new HxBusinessException("invalid appid");
		}
		HxTenantContextHolder.setCurrentTenantId(WxApp.getTenantId());

	}

	@Override
	public UserInfo doLogin(UserLoginDTO userLoginDTO) {
		WxUserDTO wxUserDTO = new WxUserDTO();
		wxUserDTO.setWxCode(userLoginDTO.getJsCode());
		wxUserDTO.setAppId(userLoginDTO.getAppId());
		Result<WxUser> result = feignWxUserService.maLogin(wxUserDTO, SecurityConstants.SOURCE_IN);
		if (Boolean.FALSE.equals(result.isOk())) {
			throw new HxBusinessException(result.getMsg());
		}
		WxUser wxUser = result.getData();
		UserInfo userInfo;
		if (StrUtil.isNotBlank(wxUser.getMallUserId())) {
			userInfo = userInfoService.getById(wxUser.getMallUserId());
		}
		else {
			userInfo = new UserInfo();
		}
		String token = UUID.randomUUID().toString();
		// 商城用户静默登录
		HxTokenInfo hxTokenInfo = new HxTokenInfo();
		hxTokenInfo.setAppId(wxUserDTO.getAppId());
		hxTokenInfo.setSessionKey(wxUser.getSessionKey());
		hxTokenInfo.setWxUserId(wxUser.getId());
		hxTokenInfo.setOpenId(wxUser.getOpenid());
		hxTokenInfo.setMallUserId(wxUser.getMallUserId());
		hxTokenInfo.setClientType(wxUserDTO.getClientType());
		hxTokenInfo.setTenantId(HxTenantContextHolder.getCurrentTenantId());
		// 将token和用户信息存入redis，并设置过期时间
		String key = CacheConstants.MALL_USER_TOKEN + token;
		redisTemplate.opsForValue()
			.set(key, JSONUtil.toJsonStr(hxTokenInfo), CacheConstants.TOKEN_TIME, TimeUnit.HOURS);
		userInfo.setHxToken(token);
		return userInfo;
	}

	@Override
	public void updateUser(UserInfo userInfo) {
		userInfoService.updateById(userInfo);
	}

	@Override
	public void distributionBindUser(UserInfo userInfo) {

	}

}
